package com.msl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.adapter.SyAdapter;
import com.msl.base.BaseActivity;
import com.msl.bean.AbSyBean;
import com.msl.scan.BroadcastConfig;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;


public class TraceabilityActivity extends BaseActivity {
    private String tag = "TraceabilityActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;
    @Bind(R.id.tv4)
    TextView tv4;
    @Bind(R.id.listview)
    ListView listview;
    private String tempcode = "";
    private AbSyBean abSyBean ;
    private SyAdapter syAdapter;

    protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                tempcode = intent.getStringExtra("scannerdata");
                waitDialog.show();
                getcode(MslApp.url + "?action=wlquery&cid=" + MslApp.sSharedPreferences.getString(MslApp.cidk, "")
                        + "&userID=" + MslApp.sSharedPreferences.getInt(MslApp.userID, 0)
                        + "&storeID=" + MslApp.sSharedPreferences.getInt(MslApp.storeID, 0)
                        + "&code=" + tempcode
                );
            }
//            else if (intent.getAction().equals("netcsucc")) {
//                tvs.setText("网络状态:正常");
//            } else if (intent.getAction().equals("netcfail")) {
//                tvs.setText("网络状态:异常");
//            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy);
        ButterKnife.bind(this);
        tvTitle.setText("溯源查询");

        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//        try {
//            Class<EditText> cls = EditText.class;
//            Method setShowSoftInputOnFocus;
//            setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",
//                    boolean.class);
//            setShowSoftInputOnFocus.setAccessible(true);
//            setShowSoftInputOnFocus.invoke(etcode, false);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
//        intentFilter.addAction("netcsucc");
//        intentFilter.addAction("netcfail");
        registerReceiver(mBroadcastReceiver, intentFilter);

//        waitDialog.show();
//        getcode(MslApp.url + "?action=wlquery&cid=" + MslApp.sSharedPreferences.getString(MslApp.cidk, "")
//                + "&userID=" + MslApp.sSharedPreferences.getInt(MslApp.userID, 0)
//                + "&storeID=" + MslApp.sSharedPreferences.getInt(MslApp.storeID, 0)
//                + "&code=" + "60673374804206"
//        );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }


    void getcode(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag, "result=" + result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    } else {
                        Log.e(tag, "获取数据失败了" + response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    abSyBean = gson.fromJson(result, AbSyBean.class);
                    if (abSyBean.getData() != null) {
                        tv1.setText("物流数码:"+abSyBean.getData().getCode());
                        tv2.setText("产品名称:"+abSyBean.getData().getProductName());
                        tv3.setText("产品编码:"+abSyBean.getData().getProductCode());
                        tv4.setText("产品型号:"+abSyBean.getData().getProductType());
                        syAdapter = new SyAdapter(TraceabilityActivity.this,abSyBean.getData().getList());
                        listview.setAdapter(syAdapter);
                    }
                    break;
                default:
                    break;

            }
        }
    };

    @OnClick({R.id.iv_title_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
        }
    }
}
